1. /* sxfxxadd.cpp by K.Tsuru */
  2. // function ID = 507 BRADIX
  3. /*****************************************************
  4. SDecimal class
  5. Provides result = m + n where m and n have the same sign.
  6. ******************************************************/
  7. #ifndef SN_H
  8. #include "sn.h"
  9. #endif
  10. static const char* func = "XXAdd";
  11. void XXAdd(const SDecimal& m, const SDecimal& n, SDecimal& result)
  12. {
  13. int msign = m.Sign(), nsign = n.Sign(), sgn = msign*nsign;
  14. if(sgn == 0){ result = m.Sign() ? m : n; return; }
  15. if(sgn < 0) m.SetError(m.SIGN_ERR, func, 507);
  16. //Three objects must have the same size.
  17. if( (m.figure.size() != n.figure.size()) || (result.figure.size() != n.figure.size()) )
  18. m.SetError(m.SYNTAX_ERR, func, 507);
  19. fType t;
  20. uint rh = result.Size()-1;
  21. uint rt = min(m.Tail(), n.Tail());
  22. fType* rv = result.figure.Elements();
  23. const fType* mv = m.ReadFigures();
  24. const fType* nv = n.ReadFigures();
  25. #ifndef NDEBUG
  26. result.figure(rh); m.figure(rh);
  27. #endif
  28. t = 0;
  29. int i;
  30. for(i = rh; i >= (int)rt; i--) {
  31. t += mv[i] + nv[i];
  32. rv[i] = t & BRADIX1;
  33. t >>= BRADIX_BITS;
  34. }
  35. //Here i = rt -1.
  36. //When rt == 0 result use up to rv[0].
  37. if(t){ //It proceeds the carry.
  38. if(i >= 0){ // rt >= 1
  39. rv[i] = (fType)t; rt--;
  40. } else m.SetError(m.OVERFLOW_ERR, func, 507);
  41. }
  42. //Clear the outside of the above roop.
  43. if(result.aHead > rh) result.figure.clear(rh+1, result.aHead);
  44. if(result.aTail < rt) result.figure.clear(result.aTail, rt-1);
  45. result.aTail = rt;
  46. //It decides the figure position.
  47. while(!rv[rh]) rh--;
  48. result.aHead = rh;
  49. result.SetSign(msign); //It must be m+n != 0.
  50. #ifndef NDEBUG
  51. result.figure(rt); result.figure(rh);
  52. #endif
  53. }

sxfxxadd.cpp : last modifiled at 2017/03/13 14:32:02(1,700 bytes)
created at 2015/12/22 16:09:56
The creation time of this html file is 2017/10/27 15:45:59 (Fri Oct 27 15:45:59 2017).